import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'Dashboard',
    component: () => import('@/views/Dashboard.vue'),
    meta: {
      title: '控制台'
    }
  },
  {
    path: '/upload',
    name: 'VideoUpload',
    component: () => import('@/views/VideoUpload.vue'),
    meta: {
      title: '视频上传'
    }
  },
  {
    path: '/tasks',
    name: 'VideoTasks',
    component: () => import('@/views/VideoTasks.vue'),
    meta: {
      title: '任务管理'
    }
  },
  {
    path: '/tasks/:id',
    name: 'TaskDetail',
    component: () => import('@/views/TaskDetail.vue'),
    meta: {
      title: '任务详情'
    }
  },
  {
    path: '/preview/:id',
    name: 'VideoPreview',
    component: () => import('@/views/VideoPreview.vue'),
    meta: {
      title: '视频预览'
    }
  },
  {
    path: '/templates',
    name: 'Templates',
    component: () => import('@/views/Templates.vue'),
    meta: {
      title: '平台模板'
    }
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('@/views/About.vue'),
    meta: {
      title: '关于平台'
    }
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/NotFound.vue'),
    meta: {
      title: '页面未找到'
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta?.title) {
    document.title = `${to.meta.title} - 智能视频内容结构重构平台`
  }
  next()
})

export default router